// component/details_img/details_img.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    is_show: Number,
    article: Object
  },

  /**
   * 组件的初始数据
   */
  data: {
    img_url: '',
    statusbar: 0,
    swiper_present: 1
  },

  onReachBottom() {
    console.log(123456);
  },
  /**
   * 组件的方法列表
   */
  lifetimes: {
    attached: function () {
      const res = wx.getSystemInfoSync()
      var statusbarH = res.statusBarHeight
      this.setData({
        statusbar: statusbarH
      })
    },
  },
  methods: {
    hide_page() {
      console.log(this.data.article);
      this.triggerEvent("show_detail", "");
      this.setData({
        is_show: 0
      });
    },
    /**
     * @param {*} e
     * 点赞以及取消点赞 
     */
    like_action(e) {
      var s = this;
      var like_data = 0;
      var review = e.currentTarget.dataset.review ? e.currentTarget.dataset.review : 0;
      if (review) {
        let like_count = review.like_count;
        review.is_liked == 1 ? like_data = 0 : like_data = 1;
      } else {
        var like_count = this.data.article.like_count;
        this.data.article.is_liked == 1 ? (like_data = 0, like_count <= 1 ? like_count = 0 : like_count--) : (like_data = 1, like_count++);
      }
      if (this.data.article.id > 0) {
        var a = this;
        getApp().core.showLoading({
          title: "加载中"
        }), getApp().request({
          url: getApp().api.weitao.like_article,
          data: {
            article_id: a.data.article.id,
            is_liked: like_data,
            review_id: review.id ? review.id : 0
          },
          success: function (t) {
            if (t.code == 0) {
              a.data.article.is_liked = like_data;
              a.data.article.like_count = like_count;
              a.setData({
                article: a.data.article
              });
            }
            getApp().core.showToast({
              title: t.msg,
              icon: 'none'
            });
          },
          complete: function () {
            getApp().core.hideLoading();
          }
        });
      } else {
        getApp().core.showToast({
          title: "系统错误",
          icon: 'none'
        });
      }
    },
    changImgUrl(e) {
      var index = e.detail.current;
      this.setData({
        img_url: this.data.article.image_content[index],
        swiper_present: e.detail.current + 1
      });

    },
    playVedio(e) {
      return false;
    },
    nextArticle(e) {
      conssole.log("下一个");
    },
    /**
     * 
     * @param {*} e 
     * 关注
     */
    focus_action(e) {
      var user_id = this.data.article.from_user,
        s = this;
      var like_data = 0;
      s.data.article.is_user_liked == 1 ? like_data = 2 : like_data = 1;
      if (user_id > 0) {
        var a = this;
        getApp().core.showLoading({
          title: "加载中"
        }), getApp().request({
          url: getApp().api.weitao.focus_user,
          data: {
            user_id: user_id,
            is_user_liked: like_data
          },
          success: function (t) {
            0 == t.code && (s.data.article.is_user_liked = like_data) && a.setData({
              article: s.data.article
            });
            console.log(s.data.article);
            getApp().core.showToast({
              title: t.msg,
              icon: 0 == t.code ? 'success' : 'none'
            });
          },
          complete: function () {
            getApp().core.hideLoading();
          }
        });
      } else{
        getApp().core.showToast({
          title: "平台账户不能关住",
          icon: 'none'
        });
      }
    },
    downloadImg: function (e) {
      wx.downloadFile({
        url: this.data.img_url ? this.data.img_url : this.data.article.image_content[0], //需要下载的图片url
        success: function (res) { //成功后的回调函数
          wx.saveImageToPhotosAlbum({ //保存到本地
            filePath: res.tempFilePath,
            success(res) {
              wx.showToast({
                title: '保存成功',
                icon: 'success',
                duration: 2000
              })
            },
            fail: function (err) {
              if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
                wx.openSetting({
                  success(settingdata) {
                    console.log(settingdata)
                    if (settingdata.authSetting['scope.writePhotosAlbum']) {
                      console.log('获取权限成功，给出再次点击图片保存到相册的提示。')
                    } else {
                      console.log('获取权限失败，给出不给权限就无法正常使用的提示')
                    }
                  }
                })
              }
            }
          })
        }
      });
    },
    //点击开始的时间  
    timestart: function (e) {
      var _this = this;
      _this.setData({
        timestart: e.timeStamp
      });
      console.log("111111")
    },
    //点击结束的时间
    timeend: function (e) {
      var _this = this;
      _this.setData({
        timeend: e.timeStamp
      });
    },

    //保存图片
    saveImg: function (e) {
      var _this = this;
      var times = _this.data.timeend - _this.data.timestart;
      if (times > 300) {
        console.log("长按");
        wx.getSetting({
          success: function (res) {
            wx.authorize({
              scope: 'scope.writePhotosAlbum',
              success: function (res) {
                console.log("授权成功");
                var imgUrl = "http://shareds.oss-cn-hangzhou.aliyuncs.com/exhibit/20180815/tmp_35d425e6e732ba516f2e8c9988706eba.jpg";
                wx.downloadFile({ //下载文件资源到本地，客户端直接发起一个 HTTP GET 请求，返回文件的本地临时路径
                  url: imgUrl,
                  success: function (res) {
                    // 下载成功后再保存到本地
                    wx.saveImageToPhotosAlbum({
                      filePath: res.tempFilePath, //返回的临时文件路径，下载后的文件会存储到一个临时文件
                      success: function (res) {
                        wx.showToast({
                          title: '成功保存到相册',
                          icon: 'success'
                        })
                      }
                    })
                  }
                })
              }
            })
          }
        })
      }
    },
  }
})